// pages/payment/payment.js
var prePayUrl = require('../../config').prePayUrl;
var dealOrderUrl = require('../../config').dealOrderUrl;
var app = getApp(); 
Page({
  data: {
    commNum:1,
    Address:{},
    payList:"",
  },
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var payList = wx.getStorageSync("payList")
    that.setData({
      Address: wx.getStorageSync("Address"),
      payList: payList,
      type: type
    })
  },
  /**
     *加减 
     */
  operNum: function (e) {
    var that = this;
    var oper = e.currentTarget.dataset.oper;
    var index = e.currentTarget.dataset.index;
    var data = that.data.payList;
    var dishNum = data.shopList[index].cart.dishNum;
    var num;
    if (oper == '-') {
      if (dishNum == 1) {
        num = 1
      } else {
        num = dishNum - 1
        data.total = data.total - 1 * data.shopList[index].dish.price
      }
    } else {
      num = dishNum + 1;
      data.total = data.total + 1 * data.shopList[index].dish.price
    }

    data.shopList[index].cart.dishNum = num
    that.setData({
      payList: that.data.payList,
    })

  },
  // 设置收货地址
  selectAddress:function(){
    var that = this;
    wx.chooseAddress({
      success:function(res){
        if (res.errMsg == "chooseAddress:ok"){
          var Address = {
            name:res.userName,
            phone : res.telNumber,
            add : `${res.cityName},${res.countyName},${res.detailInfo}`
          }
          that.setData({
            Address: Address
          });
          wx.setStorageSync("Address", Address)
        }
      },
      fail:function(error){

      }
    })  
  },
  pay:function(){
    var that = this;
    var Address = that.data.Address;
    var cartList = that.data.payList.shopList;
    var cartId = [];
    var body;
    var totalFee = that.data.payList.total;
    for (var i = 0; i < cartList.length; i++) {
      cartId.push(cartList[i].cart.id);
      body = `${body}${cartList[i].dish.name}*${cartList[i].cart.dishNum}|`
    }
    if (Address == 'not'){
      app.GBshowModalTip('提示', false, '请选择收货地址', confirmDel);
      var confirmDel = function(){}
    }else{
      wx.request({
        url: prePayUrl,
        method: "post",
        data: {
          skey: wx.getStorageSync("skey"),
          cartId: cartId,
          name: that.data.Address.name,
          phone: that.data.Address.phone,
          address: that.data.Address.add,
          body: body,
          totalFee: 1
        },
        success: function (res) {
          console.log(res)
          var outTradeNo = res.data.outTradeNo;
          if (res.data.code === 'ok') {
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: 'MD5',
              paySign: res.data.paySign,
              success: function (res) {
                console.log(res)
                wx.request({
                  url: dealOrderUrl,
                  method: "put",
                  data: {
                    skey: wx.getStorageSync('skey'),
                    state: 3,
                    outTradeNo: outTradeNo
                  },
                  success: function (res) {
                    wx.navigateTo({
                      url: 'paySucceed',
                    })
                  },
                })
              },
              fail: function (error) {
                if (error.errMsg == "requestPayment:fail cancel") {
                  wx.navigateTo({
                    url: '../indent/indent?type=0',
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  // 
  payNotpay:function(){
    var that = this;
    var order = that.data.payList.order
    wx.requestPayment({
      timeStamp: order.timeStamp,
      nonceStr: order.nonceStr,
      package: "prepay_id=" + order.prepayId,
      signType: 'MD5',
      paySign: order.paySign,
      success: function (res) {
        wx.request({
          url: dealOrderUrl,
          method: "put",
          data: {
            skey: wx.getStorageSync('skey'),
            state: 3,
            outTradeNo: outTradeNo
          },
          success: function (res) {
          },
        })
      },
      fail: function (error) {
      }
    })
  }
})